package com.guxingke.nioserver.dispathcher;

import com.guxingke.nioserver.core.HttpRequest;
import com.guxingke.nioserver.core.HttpResponse;
import com.guxingke.nioserver.event.EventAdapter;
import com.guxingke.nioserver.exception.HttpException;

/**
 * Created by guxingke on 15-8-19.
 */
public abstract class AbstartDispather extends EventAdapter {


    @Override
    public void onWrite(HttpRequest request, HttpResponse response) throws Exception {
        try {
            dispatcher(request, response);
        } catch (HttpException e) {
            response.setStatus(e.getErrorCode());
            response.setContent(e.getMessage());
        }

        response.send();
    }

    protected abstract void dispatcher(HttpRequest request, HttpResponse response) throws HttpException;
}
